package com.hitrust.gs.action;

import java.io.DataInputStream;
import org.apache.struts.upload.FormFile;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSFileUploadProcessor;
import com.hitrust.gs.form.GSManufactureFilterUploadForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.date.DateUtil;
import com.hitrust.gs.util.exception.GSException;

/**
 * 导入筛选产品信息
 */
public class GSManufactureFilterUploadAction  extends GSBaseAction
{
    public String execute(GSBaseForm aForm) throws Exception 
    {
    	boolean tFlag = true;
    	String tReturnMsg = "";
		GSManufactureFilterUploadForm tForm = (GSManufactureFilterUploadForm)aForm;
        FormFile tManufactureFilterFile = tForm.getTheFile();
        String tResult = "";
		GSOperatorBean tOperBean = (GSOperatorBean)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        
		if (tManufactureFilterFile.getFileSize() == 0) 
		{
			tFlag = false;
		    tReturnMsg = "当前文件不存在或为空!";
		}        
		if (tManufactureFilterFile.getFileName().toLowerCase().lastIndexOf("xls") == -1)
		{
			tFlag = false;
		    tReturnMsg = "文件格式错误!";
		}
		if ( tManufactureFilterFile.getFileSize() > 3145728 ){
	        tFlag = false;
	        tReturnMsg = "上传文件不能超过3M";
	    }
		
		if (tFlag)
		{
			try
			{
				//导入筛选产品信息
				tResult = GSFileUploadProcessor.batchImportFilterManufacture(new DataInputStream(tManufactureFilterFile.getInputStream()),"");
			}
			catch(GSException e)
			{
				GSLogWriter.error(e.getMessage());  
				tFlag = false;
				tReturnMsg = e.getMessage();
			}  
		}
	    
	    if (tFlag)
	    {
        	//从返回的值中取得失败和成功的数据
        	String tSuccess= tResult.split("-")[0];
        	String tFail= tResult.split("-")[1];
        	String tFailFileName = tResult.split("-")[2];
        	aForm.getRequest().setAttribute("success",tSuccess);
        	aForm.getRequest().setAttribute("fail",tFail);
        	aForm.getRequest().setAttribute("failfilename",tFailFileName);
        	return GSBaseConstants.FORWARD_SUCCESS;
	    }
	    else
	    {
			aForm.getRequest().setAttribute("exception_code",GSException.E17790);
	    	aForm.getRequest().setAttribute("exception_desc",tReturnMsg);
	    	aForm.getRequest().setAttribute("action","A401501");	
	    	return GSBaseConstants.FORWARD_FAILURE;
	    }
        
    }
}
